package com.example.dao.impl;

import com.example.dao.OrderDao;
import com.example.model.Order;
import com.example.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public int addOrder(int u_id, String t_id) {
        String sql = "insert into `order` values (? , ? )";
        int update = template.update(sql, u_id, t_id);
        return update;
    }

    @Override
    public List<Order> findAll(int u_id) {
        String sql = "  select t_id , start , end , start_time , end_time ,price from user, trip , `order` o " +
                "where user.uid = ?  and trip.id = o.t_id and user.uid = o.u_id";
        List<Order> orders = template.query(sql, new BeanPropertyRowMapper<Order>(Order.class), u_id);
        return orders;
    }

    /**
     * 删除订单
     * @param uid
     * @param id
     * @return
     */
    @Override
    public int deleteOrder(int uid, String id) {
        String sql = "delete from `order` where u_id = ? and t_id = ?";
        int row = template.update(sql, uid, id);
        return row;
    }
}
